/****************************************************
	文件：UIMgr.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/17 15:57:38
	功能：UI管理器
*****************************************************/

using System;
using UnityEngine;
using UnityEngine.UI;
using HTUtility;
using System.Collections.Generic;
using UnityEngine.EventSystems;

namespace AIBehavior
{
    public class UIMgr : MonoSingleton<UIMgr>
    {
        private GraphicRaycaster mGraphicRaycaster;
        private EventSystem mEventSystem;
        private Transform mBehaviorPanel;
        private Dropdown mBehaviorSelector;
        public void Init()
        {
            mGraphicRaycaster = GetComponent<GraphicRaycaster>();
            mEventSystem = transform.Find("EventSystem").GetComponent<EventSystem>();
            InitPanel();
            Debug.Log("UIMgr init done.");
        }
        private void InitPanel()
        {
            InitBehaviorPanel();
        }
        private void InitBehaviorPanel()
        {
            mBehaviorPanel = transform.Find("BehaviorPanel");
            InitBehaviorSelector();
        }
        private void InitBehaviorSelector()
        {
            mBehaviorSelector = mBehaviorPanel.Find("LeftUpper/BehaviorSelector").GetComponent<Dropdown>();
            List<Dropdown.OptionData> optionDataList = new List<Dropdown.OptionData>();
            foreach (string behaviorType in Enum.GetNames(typeof(BehaviorType)))
            {
                if (behaviorType == BehaviorType.None.ToString() || behaviorType == BehaviorType.NUM.ToString()) continue;
                optionDataList.Add(new Dropdown.OptionData(behaviorType));
            }
            mBehaviorSelector.AddOptions(optionDataList);  //添加Behavior选项
            mBehaviorSelector.onValueChanged.AddListener(index =>
            {
                GameRoot.Instance.ResetBehavior();
                if (index == (int)BehaviorType.None || index == (int)BehaviorType.NUM) return;
                GameRoot.Instance.SelectBehavior((BehaviorType)index);
            });
        }

        /// <summary>
        /// 检测是否点击到了UI上
        /// </summary>
        public bool CheckUIRaycast()
        {
            PointerEventData eventData = new PointerEventData(mEventSystem);
            eventData.pressPosition = Input.mousePosition;
            eventData.position = Input.mousePosition;
            List<RaycastResult> list = new List<RaycastResult>();
            mGraphicRaycaster.Raycast(eventData, list);
            return list.Count > 0;
        }
    }
}